package com.example;

public class RingList {
    public static class  Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val = val;
            this.next = null;
        }
    }
    public static Boolean calRingList(Node head){

        Node slow = head;
        Node fast = head.next;

        while(fast != null && fast.next != null){
            // 有环
            if(slow == fast){
                return true;
            }
            // 指针步长为2
            fast= fast.next.next;
            // 指针步长为1
            slow = slow.next;
        }
        return false;
    }
    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);
        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        n4.next=n5;
        n5.next=n6;
//        n6.next=n1;
        System.out.println(calRingList(n1));
    }
}
